package cn.yld.car.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class RecentlyViewedDBHelper extends SQLiteOpenHelper implements
		BaseColumns {

	// 定义数据库名称和版本号
	public static final int DB_VERSION = 1;

	public static final String DB_NAME = "recent.db";

	/*
	 * { "recordcount":"1", --返回的数据条数 "table": [ { "no":"1", --序列号 "id": "1683",
	 * --车辆ID "name": "外白内黑棕", --车辆外饰内饰简称，即这辆车的标题 "classid": "111", --系列ID
	 * "press": "115", --价格（单位W） "qhtime": "2014-04-30",
	 * --到港时间（期货才有时间，现货没有时间即为空） "vin": "", --就是车架号 "xppz": "运动包¤扩展真皮内饰¤红卡钳",
	 * --选配配置 "companyname": "", --这辆车所属经销商公司名称 "adddate":
	 * "2014-04-11 18:19:33", --这辆车的录入时间 "pinpainame": "玛莎拉蒂", --此车品牌名称
	 * "xiliename": "GHIBLI S Q4", --此车系列名称 "gangkouname": "张家港",
	 * --这辆车所属经销商公司所在港口 "xhorqh": "期货" --期货还是现货 } …… ] }
	 */

	// 定义数据库表名
	public static final String RECENT_VIEW_TABLE = "recent_view";// 表1最近浏览表

	// 表字段
	public static final String NO = "no";
	public static final String ID = "id";
	public static final String NAME = "name";
	public static final String CLASS_ID = "classid";
	public static final String PRESS = "press";
	public static final String QHTIME = "qhtime";
	public static final String VIN = "vin";
	public static final String XPPZ = "xppz";
	public static final String COMPANY_NAME = "companyname";
	public static final String ADD_DATE = "adddate";
	public static final String PIN_PAI_NAME = "pinpainame";
	public static final String XI_LIE_NAME = "xiliename";
	public static final String GANG_KOU_NAME = "gangkouname";
	public static final String XHORQH = "xhorqh";

	public static final String RECENT_VIEW_DETAIL_TABLE = "recent_view_detail";// 表名，最近浏览详细信息表

	// 表二字段
	// private String dealerid
	public static final String DEALER_ID = "dealerid";

	public static final String IMAGE1_PATH = "image1_path";
	public static final String IMAGE2_PATH = "image2_path";
	public static final String IMAGE3_PATH = "image3_path";

	public RecentlyViewedDBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		db.execSQL("DROP TABLE IF EXISTS " + RECENT_VIEW_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + RECENT_VIEW_DETAIL_TABLE);
		db.execSQL("CREATE TABLE IF NOT EXISTS  " + RECENT_VIEW_TABLE + "("
				+ _ID + "  INTEGER PRIMARY KEY  not null , " + NO + " text , "
				+ ID + " text ," + NAME + " text ," + CLASS_ID + " text, "
				+ PRESS + " text , " + QHTIME + " text , " + VIN + " text, "
				+ XPPZ + " text , " + COMPANY_NAME + " text , " + ADD_DATE
				+ " text , " + PIN_PAI_NAME + " text , " + XI_LIE_NAME
				+ " text , " + GANG_KOU_NAME + " text , " + XHORQH + " text )");

		/*
		 * d.setId(jsonObject.getString("id"));
		 * d.setNo(jsonObject.getString("no"));
		 * d.setName(jsonObject.getString("name"));
		 * d.setAdddate(jsonObject.getString("adddate"));
		 * d.setCompanyname(jsonObject.getString("companyname"));
		 * d.setGhtime(jsonObject.getString("qhtime"));
		 * d.setPress(jsonObject.getString("press"));
		 * d.setVin(jsonObject.getString("vin"));
		 * d.setXppz(jsonObject.getString("xppz"));
		 * d.setImg(jsonObject.getString("img"));
		 * d.setDealerid(jsonObject.getString("dealerid"));
		 */
		db.execSQL("CREATE TABLE IF NOT EXISTS  " + RECENT_VIEW_DETAIL_TABLE
				+ "(" + _ID + "  INTEGER PRIMARY KEY  not null , " + NO
				+ " text , " + ID + " text ," + NAME + " text ," + DEALER_ID
				+ " text, " + PRESS + " text , " + QHTIME + " text , " + VIN
				+ " text, " + XPPZ + " text , " + COMPANY_NAME + " text , "
				+ ADD_DATE + " text , " + IMAGE1_PATH + " text , "
				+ IMAGE2_PATH + " text ," + IMAGE3_PATH + " text   )");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		onCreate(db);

	}

	/**
	 * 得到可读写的数据库
	 * 
	 * @return
	 */
	public SQLiteDatabase getWritableDB() {

		return this.getWritableDatabase();
	}

	/**
	 * 得到只读数据库
	 * 
	 * @return
	 */
	public SQLiteDatabase getReadableDB() {

		return this.getReadableDatabase();

	}
}
